package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.databinding.ActivityPassWordBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;

public class PassWordActivity extends AppBarActivity {

    ActivityPassWordBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =  ActivityPassWordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("支付密码");
        initviews();
    }

    private void initviews() {
        binding.etPhone.setText(User.get().getMyInfo().getPhone());
        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
        binding.tvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendcode();
            }
        });


    }

    CountDownTimer  countDownTimer;
    private void sendcode() {


        String  phone =  binding.etPhone.getText().toString();
        if (TextUtils.isEmpty(phone))
        {
            return;
        }

        if (countDownTimer==null)
        {
            //30秒倒计时
            countDownTimer = new CountDownTimer(60000,1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    binding.tvGetCode.setClickable(false);
                    binding.tvGetCode.setText((int) (millisUntilFinished/1000)+"s");
                }

                @Override
                public void onFinish() {
                    binding.tvGetCode.setClickable(true);
                    binding.tvGetCode.setText("发送验证码");
                }
            };
        }
        countDownTimer.start();
        //发送
        sendCode(phone);

    }

    private void submit() {
        String  phone =  binding.etPhone.getText().toString();
        if (TextUtils.isEmpty(phone))
        {
            return;
        }
        String  code =  binding.etCode.getText().toString();
        if (TextUtils.isEmpty(code))
        {
            return;
        }

        String  pass =  binding.etPassword.getText().toString();
        if (TextUtils.isEmpty(pass)||pass.length()!=6)
        {
            return;
        }

        String  pass_confirm =  binding.etPasswordConfirm.getText().toString();
        if (TextUtils.isEmpty(pass_confirm))
        {
            return;
        }

        if (!pass.equals(pass_confirm))
        {
            showToast("两次密码输入不一致");
            return;
        }
        ApiFactory.getApiService().setPassWord(User.get().getMyInfo().getToken(),code,pass)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            showToast("设置成功");
                            setResult(1001);
                            finish();
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });

    }



    //发送验证码
    private void sendCode(String phone) {
        ApiFactory.getApiService().sendCode(phone,"1")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            Toast.makeText(PassWordActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });
    }
}